/*
 * Copyright © 2011 by PropSage Pte Ltd. 
 * All Rights Reserved.
 * licensing@propsage.com
 * http://www.propsage.com/terms.html
 * 
 * 
 * Javascript for /case/index
 * 
 * @author Poh Nean
 */
propsage.namespace("prestige.invoice");

prestige.invoice.view = (function($, ui){
	function initComponents()
	{	
		
		$('#emailInvoiceDialog').dialog({
			'width': 800,
			'autoOpen': false,
			'resizable': false,
			'modal': true,
			'buttons': {
				'Cancel': onDialogCancel,
				'Send Email': {
					'text': 'Send Email',
					'class': 'blue',
					'click': onSendEmailOK
				}
			},
			'open': onSendEmailOpen
		});
		
		$('#addEmailDialog').dialog({
			'width': 'auto',
			'autoOpen': false,
			'resizable': false,
			'buttons': {
				'Cancel': onDialogCancel,
				'Add Email': {
					'text': 'Add Email',
					'class': 'blue',
					'click': onAddEmailOK
				}
			},
			'open': onAddEmailOpen
		});
		
		$('#send-email-form').submit(onSendEmailFormSubmit);
		$('#add-email-form').submit(onAddEmailFormSubmit);
	}
	
	function onSendEmailFormSubmit(event) {
		event.preventDefault();
	}
	
	function onAddEmailFormSubmit(event) {
		event.preventDefault();
		
		var $form = $('#add-email-form');
		var href= $form.attr('action');
		
		var email = $("#new_email").val();
		
		if (email.length > 0) {
			ui.doAjax('POST', href, $form.serialize(), onAddEmailSuccess, onAddEmailFailure);
			ui.showPleaseWait();
			$('#addEmailDialog').dialog("close");	
		} else {
			window.alert('Please enter an email address');
		}	
	}
	
	
	function initEventHandlers() {  
		$('#btnEmail').on('click', onBtnEmailClick)
		$('#btnAddEmail').on('click', onBtnAddEmailClick)
	}
	
	function onAddEmailOpen() {
		$('#new_email').val('');
	}
	
	function onBtnAddEmailClick(event) {
		$('#addEmailDialog').dialog("open");
	}
	
	function onAddEmailOK() {
		$('#add-email-form').submit();	
	}
	
	function onAddEmailSuccess(response) {
		ui.hidePleaseWait();
		
		if (response.result == 'success') {
			var newOption = $('<option />')
			.val(response.new_email)
			.text(response.new_email)
			.prop('selected', true);
			
			$('#EmailForm_recipients')
			.append(newOption)
			.trigger("liszt:updated");
			
			var emailDiv = $('<div />').text(response.new_email);
			var emaillink = '<a tabindex="-1" class="select2-search-choice-close" onclick="return false;" href="#"></a>';
			var newli = $('<li />')
					.addClass('select2-search-choice')
					.append(emailDiv)
					.append(emaillink);
			$('ul.select2-choices')
					.append(newli);
		}
	}
	
	function onAddEmailFailure() {		
		ui.hidePleaseWait();
	}
	
	function onBtnEmailClick(event) {
		event.preventDefault();
		
		$('#emailInvoiceDialog').dialog("open");		
	}
	
	function onEmailMenuItemSelect(item) {
		var action = $(item).attr('action');
		
		switch (action) {
			case 'emailInvoice':
				$('#emailInvoiceDialog').dialog("open");
				break;
			case 'emailReminder':
				break;
		}
	}
	
	function onEmailInvoiceActionClick() {
		$('#emailInvoiceDialog').dialog("open");
	}
	
	function onSendEmailOK() {
		var $form = $('#send-email-form');
		var href= $form.attr('action');
		
		var recipientsSelected = $('#EmailForm_recipients').val();
		
		if (recipientsSelected == null || recipientsSelected.length == 0) {
			window.alert("Please select one or more recipients to send to.");
		} else {		
			ui.doAjax('POST', href, $form.serialize(), onSendEmailSuccess, onSendEmailFailure);
			ui.showPleaseWait();
		}
	}
	
	function onSendEmailSuccess(response) {
		ui.hidePleaseWait();	
			
		if (response.result == 'success') {
			$('#emailInvoiceDialog').dialog("close");
		}
	}
	
	function onSendEmailFailure() {
		ui.hidePleaseWait();
	}
	
	function onSendEmailOpen() {
		if ($('#EmailForm_recipients option').size() == 0) {
			$('#btnAddEmail')
			.qtip({
				content: {
					text: "The bill to customer does not have any email addresses. Click here to add one."
				},				
				show: {
					event: false, // Don't specify a show event...
					ready: true // ... but show the tooltip when ready
				}
			})
			.qtip("show");
		}
	}
	
	function onDialogCancel() {
		$(this).dialog('close');
	}
	
	
	$(document).ready(function(){
		initComponents();
		initEventHandlers();
	});
	
	return {
		onEmailInvoiceActionClick: onEmailInvoiceActionClick
	}
    
}(jQuery, propsage.ui));